package no.ntnu.idi.evaluering;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import no.ntnu.idi.deid.documentunits.Document;
import no.ntnu.idi.deid.documentunits.Token;
import no.ntnu.idi.deid.documentunits.TokenIterator;
import no.ntnu.idi.deid.preprocessor.Documents;

public class EvaluationFile {
		
	private EvaluationFile(File file) {;
	}
		
	private static String evaluationString(Token token) {
		int sensvalue = token.isSensitiv()? 1 : 0;
		String orginalToken;
		if(token.getOrginal().endsWith(EvaluationConstants.SENSITIVTYMARK)) {
			orginalToken = token.getOrginal().substring(0, token.getOrginal().indexOf(EvaluationConstants.SENSITIVTYMARK));
		}
		else {
			orginalToken =  token.getOrginal();
		}
		return orginalToken+EvaluationConstants.DELIMITER+sensvalue;
	}
	
	public static void generateFileFrom(Document document) {
		String filename = document.getName();
		generateFileFrom(document, new File(filename));
	}
	
	public static void generateFileFrom(Document document, String filenamePrefix) {
		String filename = filenamePrefix+document.getName();
		generateFileFrom(document, new File(filename));
	}
	
	public static void generateFileFrom(Document document, File file) {
		Writer writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file), "utf-8"));
			TokenIterator i = document.getTokenIterator();
			while (i.hasNext()) {
				writer.write(evaluationString(i.next()) + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public static void generateFilesFrom(Documents documents, String filenamePrefixes) {
		for(Document document: documents) {
			generateFileFrom(document,filenamePrefixes);
		}
	}
	
	public static void generateFilesFrom(Documents documents) {
		for(Document document: documents) {
			generateFileFrom(document);
		}
	}
	
	private static String getKey(String line) {
		return line.substring(0,line.indexOf(EvaluationConstants.DELIMITER));
	}
	
	private static boolean getValue(String line) {
		String value = line.substring(line.indexOf(EvaluationConstants.DELIMITER)+EvaluationConstants.DELIMITER.length());
		return convertStringToBool(value);
	}
	
	public static boolean  convertStringToBool(String value) throws IllegalArgumentException {
		if(value.equals("1")) {
			return true;
		}
		else if(value.equals("0")) {
			return false;
		}
		else {
			throw new IllegalArgumentException("Inputen i fila er ikke 0 eller 1");
		}
	}
	
	public static List<EvaluationEntity> readFile(File file) {
		List<EvaluationEntity> resultList = new ArrayList<EvaluationEntity>();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				resultList.add(new EvaluationEntity(getKey(line),getValue(line)));
			}
			br.close();
		} catch ( IOException e) {
			if(br != null) {
				try {
					br.close();
				} catch (IOException e1) {
					System.err.println("EvaluationFile " + file.getName()+": not closed");
				}
			}
			e.printStackTrace();
		}
		return resultList;
	}
}
